{% extends "base.html" %}

{% block title %}积分管理 - 后台管理系统{% endblock %}

{% block content %}
<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
    <h1 class="h2">积分管理</h1>
    <div class="btn-toolbar mb-2 mb-md-0">
        <div class="btn-group me-2">
            <button type="button" class="btn btn-sm btn-outline-secondary" onclick="location.reload()">
                <i class="fas fa-refresh"></i> 刷新数据
            </button>
        </div>
    </div>
</div>

<!-- 积分统计卡片 -->
<div class="row mb-4">
    <div class="col-xl-4 col-md-6 mb-4">
        <div class="card card-stats-success h-100">
            <div class="card-body">
                <div class="row">
                    <div class="col">
                        <h5 class="card-title text-uppercase text-muted mb-0">总获得积分</h5>
                        <span class="h2 font-weight-bold mb-0">{{ "{:,}".format(stats[0] or 0) }}</span>
                    </div>
                    <div class="col-auto">
                        <div class="icon icon-shape bg-white text-success rounded-circle shadow">
                            <i class="fas fa-plus-circle fa-2x"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    
    <div class="col-xl-4 col-md-6 mb-4">
        <div class="card card-stats-warning h-100">
            <div class="card-body">
                <div class="row">
                    <div class="col">
                        <h5 class="card-title text-uppercase text-muted mb-0">总消费积分</h5>
                        <span class="h2 font-weight-bold mb-0">{{ "{:,}".format(stats[1] or 0) }}</span>
                    </div>
                    <div class="col-auto">
                        <div class="icon icon-shape bg-white text-warning rounded-circle shadow">
                            <i class="fas fa-minus-circle fa-2x"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    
    <div class="col-xl-4 col-md-6 mb-4">
        <div class="card card-stats h-100">
            <div class="card-body">
                <div class="row">
                    <div class="col">
                        <h5 class="card-title text-uppercase text-muted mb-0">积分记录数</h5>
                        <span class="h2 font-weight-bold mb-0">{{ "{:,}".format(stats[2] or 0) }}</span>
                    </div>
                    <div class="col-auto">
                        <div class="icon icon-shape bg-white text-primary rounded-circle shadow">
                            <i class="fas fa-list fa-2x"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- 积分记录列表 -->
<div class="card">
    <div class="card-header">
        <h5 class="card-title mb-0">积分变更记录</h5>
    </div>
    <div class="card-body">
        {% if points_log %}
            <div class="table-responsive">
                <table class="table table-hover">
                    <thead>
                        <tr>
                            <th>ID</th>
                            <th>用户</th>
                            <th>手机号</th>
                            <th>积分变更</th>
                            <th>变更前</th>
                            <th>变更后</th>
                            <th>变更类型</th>
                            <th>变更原因</th>
                            <th>操作员</th>
                            <th>时间</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for log in points_log %}
                            <tr>
                                <td>{{ log[0] }}</td>
                                <td>{{ log[1] }}</td>
                                <td>{{ log[2] or '-' }}</td>
                                <td>
                                    {% if log[3] > 0 %}
                                        <span class="text-success">+{{ log[3] }}</span>
                                    {% else %}
                                        <span class="text-danger">{{ log[3] }}</span>
                                    {% endif %}
                                </td>
                                <td>{{ log[4] }}</td>
                                <td>{{ log[5] }}</td>
                                <td>
                                    {% if log[6] == 1 %}
                                        <span class="badge bg-primary">签到</span>
                                    {% elif log[6] == 2 %}
                                        <span class="badge bg-success">充值</span>
                                    {% elif log[6] == 3 %}
                                        <span class="badge bg-warning">消费</span>
                                    {% elif log[6] == 4 %}
                                        <span class="badge bg-info">奖励</span>
                                    {% elif log[6] == 5 %}
                                        <span class="badge bg-secondary">管理员</span>
                                    {% elif log[6] == 7 %}
                                        <span class="badge bg-success">注册赠送</span>
                                    {% else %}
                                        <span class="badge bg-light text-dark">其他</span>
                                    {% endif %}
                                </td>
                                <td>{{ log[7] }}</td>
                                <td>{{ log[8] }}</td>
                                <td>{{ log[9].strftime('%Y-%m-%d %H:%M:%S') }}</td>
                            </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
            
            <!-- 分页 -->
            {% if total_pages > 1 %}
                <nav aria-label="积分记录分页">
                    <ul class="pagination justify-content-center">
                        {% if page > 1 %}
                            <li class="page-item">
                                <a class="page-link" href="{{ url_for('points', page=page-1) }}">上一页</a>
                            </li>
                        {% endif %}
                        
                        {% for p in range(1, total_pages + 1) %}
                            {% if p == page %}
                                <li class="page-item active">
                                    <span class="page-link">{{ p }}</span>
                                </li>
                            {% elif p <= 3 or p >= total_pages - 2 or (p >= page - 2 and p <= page + 2) %}
                                <li class="page-item">
                                    <a class="page-link" href="{{ url_for('points', page=p) }}">{{ p }}</a>
                                </li>
                            {% elif p == 4 or p == total_pages - 3 %}
                                <li class="page-item disabled">
                                    <span class="page-link">...</span>
                                </li>
                            {% endif %}
                        {% endfor %}
                        
                        {% if page < total_pages %}
                            <li class="page-item">
                                <a class="page-link" href="{{ url_for('points', page=page+1) }}">下一页</a>
                            </li>
                        {% endif %}
                    </ul>
                </nav>
            {% endif %}
        {% else %}
            <div class="text-center py-4">
                <i class="fas fa-inbox fa-3x text-muted mb-3"></i>
                <p class="text-muted">暂无积分记录</p>
            </div>
        {% endif %}
    </div>
</div>
{% endblock %}